# 创建测试类
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
from common.dbutil import DBUtil


class TestReg(object):
    def setup_class(self):
        # 清理数据
        # 打开浏览器
        self.chrome = webdriver.Chrome()
        # 窗口最大化
        self.chrome.maximize_window()

    def setup_method(self):
        # 输入测试页面网址
        self.chrome.get("http://121.43.169.97:8081/common/member/reg")
        # 清理测试数据
        DBUtil().exe_sql("DELETE from mb_member where phone='18734494804';")
        DBUtil().exe_sql("DELETE from mb_member_info where member_name='18734494804';")
        DBUtil().exe_sql("DELETE from mb_member_login_log where member_name='18734494804';")
        DBUtil().exe_sql("DELETE from mb_member_register_log where member_name='18734494804';")

    # 注册成功
    try:
        def test01_success(self):
            # 测试步骤
            # 1、输入手机号码
            element = self.chrome.find_element(By.CSS_SELECTOR, "#phone")
            element.send_keys("18734494804")
            # 2、输入密码
            self.chrome.find_element(By.CSS_SELECTOR, "#password").send_keys("www.13133")
            # 3、输入图片验证码
            self.chrome.find_element(By.CSS_SELECTOR, "#verifycode").send_keys("8888")
            # 4、点击获取短信验证码
            self.chrome.find_element(By.CSS_SELECTOR, "#get_phone_code").click()
            time.sleep(2)
            # 5、输入短信验证码内容
            self.chrome.find_element(By.CSS_SELECTOR, "#phone_code").send_keys("666666")
            # 6、勾选协议
            # 7、点击免费注册按钮
            self.chrome.find_element(By.CSS_SELECTOR, ".lg-btn").click()
            time.sleep(2)
    except Exception as e:
        print(e)

    def teardown_methode(self):
        # 8、截图保存关键截图
        self.chrome.get_screenshot_as_file("../image/reg.png")
        # 9、断言
        # 等于 assert 预期结果 == 实际结果
        # 包含 assert 预期结果 in 实际结果
        assert "注册成功" in self.chrome.find_element(By.TAG_NAME, "h1").text
        assert "18734494804" in self.chrome.find_element(By.LINK_TEXT, "18734494804").text

    def teardown_class(self):
        # 关闭浏览器
        time.sleep(3)
        self.chrome.quit()

    # 注册失败
    # 注册失败
